package objects;

import java.awt.Graphics;

import sfx.SoundManager;
import sfx.SoundX;
import core.Main;
import core.Vector2;

public class Explosion extends Entity {
	
	public SoundX explosionSound;
	
	protected int size;
	
	protected int imageX, imageY;
	protected long lastChange;
	
	public static final int TIME = 75;
	
	public Explosion(Vector2 pos, int size) {
		super(pos, 0.0, new Vector2(size, size));
		
		this.size = size;
		this.imageX = 0;
		this.imageY = 0;
		this.lastChange = System.currentTimeMillis();
		
		explosionSound = new SoundX(Main.soundManager.getSound(SoundManager.EXPLOSION), pos);
		explosionSound.play();
	}
	
	@Override
	public void tick() {
		if(System.currentTimeMillis() - lastChange > TIME) {
			if(imageX < 4 && imageY < 3) {
				imageX++;
			} else if(imageX == 4 && imageY < 2) {
				imageY++;
				imageX = 0;
			} else {
				removed = true;
			}
			lastChange = System.currentTimeMillis();
		}
//		if(Main.SOUND_ACTIVE && !Main.soundManager.isPlaying(SoundManager.WTFBOOM)) {
//			Main.soundManager.play(SoundManager.WTFBOOM);
//		}
	}
	
	@Override
	public void render(Graphics g) {
		int x = (int)((pos.x - size / 2 - Main.mapPos.x) * Main.zoom);
		int y = (int)((pos.y - size - Main.mapPos.y) * Main.zoom);
		
		g.drawImage(Main.explosion[imageX][imageY], x, y, (int)(size * Main.zoom), (int)(size * Main.zoom), null);
	}
	
	@Override
	public Vector2 getSize() {
		return new Vector2(size, size);
	}
}
